package com.cpl.common.dao;
import java.util.Map;
import com.cpl.common.dao.BaseEntity;
import com.cpl.common.exception.CPLDaoException;


public abstract class CPLDaoSupport extends org.springframework.orm.hibernate3.support.HibernateDaoSupport  {

	
	
	private Map<String,String> dbQuery;

	/**
	 * This constant is used as a transformation flag; entities can be converted
	 * automatically into value objects or other types, different methods in a
	 * class implementing this interface support this feature: look for an
	 * <code>int</code> parameter called <code>transform</code>.
	 * <p>
	 * This specific flag denotes no transformation will occur.
	 */
	public final static int T_NONE = 0;

	/**
	 * This constant is used as a transformation flag; entities can be converted
	 * automatically into value objects or other types, different methods in a
	 * class implementing this interface support this feature: look for an
	 * <code>int</code> parameter called <code>transform</code>.
	 * <p>
	 * This specific flag denotes entities must be transformed into objects of
	 */
	public final static int T_CPLVO = 1;

	/**
	 * It loads the entity of type com.mo.CPL.dao.BaseEntity.
	 * 
	 * @param item_id
	 * @param classEntity
	 * @return com.mo.CPL.dao.BaseEntity
	 * @throws CPLDaoException
	 */
	public BaseEntity load(final java.lang.Long item_id , Class classEntity)throws CPLDaoException
	{
		return (BaseEntity)this.load(T_NONE, item_id, classEntity);
	}

	public BaseEntity load(final String id , Class classEntity)throws CPLDaoException
	{
		return (BaseEntity)this.load(T_NONE,id, classEntity);
	}
	/**
	 * @return the dbQuery
	 */
	public String getDbQueryMapValue(String key) {
		return dbQuery.get(key);
	}



	/**
	 * @param dbQuery the dbQuery to set
	 */
	public void setDbQuery(Map dbQuery) {
		this.dbQuery = dbQuery;
	}
	
	/**
	 * It loads the entity of typeBaseEntity.
	 * @param item_id
	 * @param classEntity
	 * @param transform
	 * @return Object
	 * @throws CPLDaoException
	 */
	private java.lang.Object load(final int transform, final java.lang.Long item_id, Class classEntity) throws  CPLDaoException 
	{
		if (item_id == null){

			throw new CPLDaoException("CPLSupport.load - 'item_id' can not be null");
		}
		try{
			final java.lang.Object entity = this.getHibernateTemplate().get(classEntity, item_id);
			return entity;
		}catch(Exception e)
		{
			throw new CPLDaoException(e);

		}

	}
	
	private java.lang.Object load(final int transform, final String item_id, Class classEntity) throws  CPLDaoException 
	{
		if (item_id == null){

			throw new CPLDaoException("CPLSupport.load - 'item_id' can not be null");
		}
		try{
			final java.lang.Object entity = this.getHibernateTemplate().get(classEntity, item_id);
			return entity;
		}catch(Exception e)
		{
			throw new CPLDaoException(e);

		}

	}


	/**
	 * loads the collection of entity via making a call to loadAll.
	 * @param
	 * @return
	 * @throws
	 * @param classEntity
	 * @return Collection
	 * @throws CPLDaoException
	 */
	public java.util.Collection<BaseEntity> loadAll(Class classEntity)throws CPLDaoException
	{
		return this.loadAll(T_NONE, -1, -1, classEntity);
	}


	/**
	 * Do the actual implementation of loading the collection of entity.
	 * 
	 * @param transform
	 * @param pageNumber
	 * @param pageSize
	 * @param classEntity
	 * @return Collection
	 * @throws CPLDaoException
	 */
	private java.util.Collection<BaseEntity> loadAll(final int transform, final int pageNumber, final int pageSize, Class classEntity)throws CPLDaoException
	{
		try
		{
			final org.hibernate.Criteria criteria = this.getSession(false).createCriteria(classEntity);
			if (pageNumber > 0 && pageSize > 0){	
				criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
				criteria.setMaxResults(pageSize);        
			}
			final java.util.Collection<BaseEntity> results = criteria.list();
			//this.transformEntities(transform, results);
			return results;
		}
		catch (org.hibernate.HibernateException ex)
		{
			throw new CPLDaoException(ex);
		}
	}



	/**
	 * Introduced to display the result pagewise.
	 * @param pageNumber
	 * @param pageSize
	 * @return int
	 */
	protected int calculateFirstResult(int pageNumber, int pageSize)
	{
		int firstResult = 0;
		if (pageNumber > 0){
			firstResult = (pageNumber - 1) * pageSize;
		}
		return firstResult;
	}



	/**
	 * call the other overload create method in order to insert the entity.
	 * @param base
	 * @returnBaseEntity
	 * @throws CPLDaoException
	 */
	public BaseEntity create(BaseEntity base)throws CPLDaoException
	{
		return (BaseEntity)this.create(T_NONE, base);
	}

	/**
	 * 
	 * Do the actual implementation for inserting a record into the entity.
	 * @param transform
	 * @param base
	 * @return Object
	 * @throws CPLDaoException
	 */
	private java.lang.Object create(final int transform, final BaseEntity base)throws CPLDaoException
	{
		try {
			if (base == null){

				throw new CPLDaoException("CPLSupport.create - 'Base entity' can not be null");
			}
			this.getHibernateTemplate().save(base);
			return base;

		} catch(Exception ex) {
			throw new CPLDaoException(ex);
		}
	}



	/**
	 * call the other overload create method in order to insert the collection of record into the entity.
	 * @param entities
	 * @return Collection
	 * @throws CPLDaoException
	 */
	public java.util.Collection create(final java.util.Collection entities)throws CPLDaoException
	{
		return create(T_NONE, entities);
	}



	/**
	 * Do the actual implementation for inserting a collection of record into the entity.
	 * @param transform
	 * @param entities
	 * @return Collection
	 * @throws CPLDaoException 
	 * @throws CPLDaoException
	 */
	private java.util.Collection create(final int transform, final java.util.Collection entities) throws CPLDaoException
	{
		try{
			if (entities == null)
			{
				throw new CPLDaoException("CPLSupport.create - 'record to be updated' can not be null");
			}
			this.getHibernateTemplate().executeWithNativeSession(
					new org.springframework.orm.hibernate3.HibernateCallback() {
						public java.lang.Object doInHibernate(org.hibernate.Session session)
						throws org.hibernate.HibernateException {

							for (java.util.Iterator entityIterator = entities.iterator(); entityIterator.hasNext();) {
								try {
									create(transform, (BaseEntity)entityIterator.next());
								} catch (CPLDaoException e) {
									//logger.logMessage("debug","CPLDaoSupport","create", CREATE_EXCEPTION) ; 
								}
							}
							return null;
						}
					});
			return entities;
		}catch(Exception ex) {

			throw new CPLDaoException(ex);
		}
	}


	/**
	 * Update the entity for a particular record Id
	 * @param master
	 * @throws CPLDaoException
	 */
	public void update(BaseEntity master)throws CPLDaoException
	{
		if (master == null){

			throw new CPLDaoException("CPLSupport.update - 'record to be updated' can not be null");
		}
		try{

			this.getHibernateTemplate().update(master);

		}catch(Exception e){
			throw new CPLDaoException(e);
		}
	}


	/**
	 * Update the entity for a colelction of record Id
	 * @param master
	 * @throws CPLDaoException
	 */
	public void update(final java.util.Collection entities)throws CPLDaoException
	{
		try{
			if (entities == null)
			{
				throw new IllegalArgumentException(
						"CPLSupport.update - 'entities to be updated' can not be null");
			}
			this.getHibernateTemplate().executeWithNativeSession(
					new org.springframework.orm.hibernate3.HibernateCallback()
					{
						public java.lang.Object doInHibernate(org.hibernate.Session session)
						throws org.hibernate.HibernateException
						{
							for (java.util.Iterator entityIterator = entities.iterator(); entityIterator.hasNext();)
							{
								try {
									update((BaseEntity)entityIterator.next());
								} catch (CPLDaoException e) {
									//logger.logMessage("debug","CPLDaoSupport","Update", "Exception occured while updating entity") ;
								}
							}
							return null;
						}
					});
		}catch(Exception ex){
			throw new CPLDaoException(ex);
		}
	}



	/**
	 * Remove the entity.
	 * @param master
	 * @throws CPLDaoException
	 */
	public void remove(BaseEntity base)throws CPLDaoException
	{
		if (base == null)
		{
			throw new IllegalArgumentException(
					"CPLSupport.remove - 'Base entity to be removed' can not be null");
		}
		try{

			this.getHibernateTemplate().delete(base);

		}catch(Exception e){
			throw new CPLDaoException(e);
		}
	}


	/**
	 * Remove the entity for a particular record Id
	 * @param master
	 * @throws CPLDaoException
	 */
	public void remove(java.lang.Long item_id, Class entity)throws CPLDaoException
	{

		if (item_id == null)
		{
			throw new IllegalArgumentException(
					"CPLSupport.remove - 'entity Id' can not be null");
		}
		BaseEntity entityBase = this.load(item_id,entity);
		try{        
			if (entity != null){
				this.remove(entityBase);
			}
		}catch(Exception e){
			throw new CPLDaoException(e);
		}
	}



}
